<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.w3.org/1999/xhtml">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/semantic-ui@2.4.2/dist/semantic.min.css">
    <link rel="stylesheet" href="../../static/css/my.css" th:href="@{/css/my.css}">
    <title>登录</title>
</head>
<body class="m-bloght">
<br>
<br>
<br>
<br>
<br>
<div class="m-container-small m-padded-tb-massive" style="max-width: 30em !important;">
    <div class="ur container">
        <div class="ui middle aligned center aligned grid">
            <div class="column">
                <h2 class="ui teal image header">
<!--                    <img src="assets/images/logo.png" class="image">-->
                    <div class="content">
                        登录
                    </div>
                </h2>
                <form class="ui large form" method="post" action="#">
                    <div class="ui segment">
                        <div class="field">
                            <div class="ui left icon input">
                                <i class="user icon"></i>
                                <input type="text" name="username" placeholder="Username">
                            </div>
                        </div>
                        <div class="field">
                            <div class="ui left icon input">
                                <i class="lock icon"></i>
                                <input type="password" name="password" placeholder="Password">
                            </div>
                        </div>
                        <div class="ui fluid large teal submit button">Login</div>
                    </div>

                    <div class="ui error mini message"></div>
                    <div class="ui mini negative message" th:unless="${#strings.isEmpty(message)}" th:text="${message}">用户名和密码错误！</div>
                </form>

                <div class="ui message">
                    没有账号 ? <a href="#">Sign Up</a>
                </div>
            </div>
        </div>
    </div>
</div>


<script src="http://code.jquery.com/jquery-2.1.1.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/semantic-ui@2.4.2/dist/semantic.min.js"></script>
<script>
    $(".button").on("click",function (){
        var username=$("input[name=username]").val();
        var password=$("input[name=password]").val();
        // alert(username+" "+password);
        // alert("123");
        $.ajax({
            url:"/user/login",
            data: JSON.stringify({
                "username":username,
                "password":password
            }),
            dataType:"json",
            contentType:"application/json",
            async:true,
            type:"POST",
            success: function (data){
               if (data.data == "登录失败，用户名不存在！"){
                   alert("登录失败，用户名不存在！");
               }
                if (data.data == "登录失败，用户密码不正确！"){
                    alert("登录失败，用户密码不正确！");
                }
                if (data.data == "登录失败，未知错误！"){
                    alert("登录失败，未知错误！");
                }
                if (data.code == "200"){
                    alert("登录成功！");
                    window.location.href='/home';
                }
            }
        })
    })
</script>
<script>
    /**表单验证**/
    $(".ui.form").form({
        fields: {
            username: {
                identifier: 'username',
                rules: [{
                    type: 'empty',
                    prompt: '请输入用户名！'
                }]
            },
            password: {
                identifier: 'password',
                rules: [{
                    type: 'empty',
                    prompt: '请输入密码！'
                }]
            }
        }
    });
</script>
</body>

</html>